
<style type="text/css">
    .wrong {color: red}
</style>
<?
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/ContractReport.css');
$html='';
$html.='<div class="details">';
$html.='<h3>QSI, INC</h3>';
$html.='<h1>Sales Tax by State - Invoice Report</h1>';
if($startDate!=null && $endDate!=null){
    $html.='<h4>'.$startDate.' to '.$endDate.'</h4>';
}
if(isset($departments) && count($departments)>=1){
    $html.='<h4>Inc, Depts: ';
    foreach($departments as $index=>$department){
        $html.=$department.' - '.SalesDepartments::getText($department);
        if($index<count($departments)-1){
            $html.=', ';
        }
    }
    $html.='</h4>';
}

$html.='</div>';

$itemTable=new CI_Table();
    $itemTable->set_template(array(
        'table_open'=>'<table width="1325" cellspacing="0" cellpadding="0">'
    ));
    $itemTable->set_heading(array(
       'Type','Date','Invoice','County - State','Dept',
        array(
            'data'=>'CustNo - LocNo - LocName',
            'style'=>'width:405px;'
        ),
        'Amount','Tax','Tax Percent','Taxable','Tax Test','Result'
    ));
$summaryRows=array();    
foreach($viewData as $stateName=>$stateData){
    
    
    $itemTable->add_row('<br/>');
    $itemTable->add_row("<b>$stateName</b>");
    foreach($stateData['items'] as $item){
        $state='';
        if($item['county']!=""){
            $state = $item['county'].' - '.$item['state'];
        }else{
            $state =$item['state'];
        }
        $wrongTestSignal='';
        if(  (abs($item['test']) - 0) > 0.01){
          $class ='wrong';
        }
          else
              $class = 'ok';
        $itemTable->add_row(array(
            array(
            'data'=>'v'.$item['version'].' - '.$item['invoicetype'],
             "class"=>"$class"
                 ),
            array (
           'data'=> $item['fdate']   ,
                "class"=>"$class"
            ),
            array (
                'data'=> $item['invoice']   ,
                "class"=>"$class"
            ),
            array (
                'data'=>$state ,
                "class"=>"$class"
            ),
            array (
                'data'=>  $item['department'] ,
                "class"=>"$class"
            ),
            array (
                'data'=>   $item['custno'] . ' - ' . $item['locno'] . ' - ' . $item['locname'] ,
                "class"=>"$class"
            ),

            array(
                'data'=>number_format($item['amountcharge'],2),
                'align'=>'right',
                "class"=>"$class"
                ),
            array(
                'data'=>number_format($item['tax'],2),
                'align'=>'right',
                "class"=>"$class"
            ),
            array(
                'data'=>number_format($item['taxrate'],2),
                'align'=>'right',
                "class"=>"$class"
            ),
            array(
                'data'=>number_format($item['exempt'],2),
                'align'=>'right',
                "class"=>"$class"
            ),
            array(
                'data'=>number_format($item['taxtest'],2),
                'align'=>'right',
                "class"=>"$class"
            ),
            array(
                'data'=>number_format($item['test'],2),
                'align'=>'right',
                "class"=>"$class"
            )            
        ));
    }
    $summaryRow=array(
            "<b>$stateName Total</b>",
            '','','','','',
            array(
                'data'=>number_format($stateData['amount'],2),
                'align'=>'right'
            ),
            array(
                'data'=>number_format($stateData['tax'],2),
                'align'=>'right'
            ),
            '',
            array(
                'data'=>number_format($stateData['exempt'],2),
                'align'=>'right'
            ),
            array(
                'data'=>number_format($stateData['taxtest'],2),
                'align'=>'right'
            ),
            array(
                'data'=>number_format($stateData['test'],2),
                'align'=>'right'
            ));
    $summaryRows[] = $summaryRow;
    $itemTable->add_row(
            $summaryRow
     );
              
}
$itemTable->add_row('<br/>');
$itemTable->add_row(
        '<b>Summary</b>
            <br/>'
        ); 
$itemTable->add_row(array(
    'data'=>'<hr/>',
    'colspan'=>'12'
));
foreach($summaryRows as $summaryRow){
    $itemTable->add_row(
        $summaryRow
        );
}
$itemTable->add_row(array(
    'data'=>'<hr/>',
    'colspan'=>'12'
));
$itemTable->add_row(
        '<b>Grand Total</b>',
        '','','','','',
        array(
            'data'=>number_format($grandAmount,2),
            'align'=>'right'
        ),
        array(
            'data'=>number_format($grandTax,2),
            'align'=>'right'
        ),
        '',
        array(
            'data'=>number_format($grandExempt,2),
            'align'=>'right'
        ),
        
        array(
            'data'=>number_format($grandTaxTest,2),
            'align'=>'right'
        ),
        array(
            'data'=>number_format($grandTest,2),
            'align'=>'right'
        )
 );
$html.=$itemTable->generate();
echo $html;
